<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="./css/style.css" />
    <script type="text/javascript" src="../../lib/aircity/ac_conf.js"></script>
    <script type="text/javascript" src="../../lib/aircity/ac.min.js"></script>
    <script type="text/javascript" src="../../lib/vue3.js"></script>
  </head>
  <body>
    <div id="app">
      <div id="player" style="height: 100%"></div>
      <div class="control">
        <div class="top">
          <div>开敞度分析</div>
        </div>
        <div class="params">
          <div class="title">参数设置</div>
          <div class="item">
            <div class="label">展示半径</div>
            <div class="content">
              <input type="number" v-model="options.radius" style="width: 180px" :min="1" :step="10" />
              m
            </div>
          </div>
          <div class="item">
            <div class="label">不透明度</div>
            <div class="content">
              <input type="number" v-model="options.opacity" style="width: 196px" :min="0" :max="1" :step="0.1" />
            </div>
          </div>
          <div class="item">
            <div class="label">可见颜色</div>
            <div class="content">
              <input type="color" v-model="options.visibleColor" style="width: 100%" />
            </div>
          </div>
          <div class="item">
            <div class="label">遮挡颜色</div>
            <div class="content">
              <input type="color" v-model="options.invisibleColor" style="width: 100%" />
            </div>
          </div>

          <div class="button-box">
            <button @click="start()">开始开敞度分析</button>
          </div>
          <div class="button-box">
            <button @click="stop()" class="sunlight stop">停止开敞度分析</button>
          </div>
        </div>
      </div>
    </div>

    <script type="text/javascript">
      // 按需获取
      const { createApp, reactive, watch } = Vue

      // vue组件
      const vueComposition = {
        setup() {
          // 开敞度分析 代码块
          const options = reactive({
            radius: 50, // 展示半径，单位：米，取值范围：[0.01~任意正数]
            opacity: 0.5, //透明度，取值范围：[0~1]，默认值：0.5
            visibleColor: '#00ff00', // 可见区域的颜色
            invisibleColor: '#ff0000' // 不可见区域的颜色
          })

          // 分析状态，true为正在分析，false则没有进行分析
          let analysisState = false

          /**
           * 开始开敞度分析
           */
          const start = () => {
            // 设置分析状态为 true
            analysisState = true
            // 开启分析
            __g.tools.startViewDomeAnalysis(JSON.parse(JSON.stringify(options)))
          }

          /**
           * 防抖
           * @param func setTimeout回调
           * @param wait setTimeout时间
           * @returns {(function(): void)|*}
           */
          const debounce = (func, wait) => {
            let timeout = null
            return function () {
              if (analysisState) {
                clearTimeout(timeout)
                timeout = setTimeout(() => {
                  func()
                }, wait)
              }
            }
          }

          // options监听，执行防抖的 开启分析事件
          watch(options, debounce(start, 500), {
            deep: true
          })

          /**
           * 停止开敞度分析
           */
          const stop = () => {
            // 设置分析状态为 false
            analysisState = false
            // 关闭分析
            __g.tools.stopViewDomeAnalysis()
          }

          return {
            options,
            start,
            stop
          }
        }
      }

      // 挂载vue
      const app = createApp(vueComposition).mount('#app')

      /**
       * 初始化三维
       * @constructor
       */
      const OnLoad = () => {
        new DigitalTwinPlayer(HostConfig.Player, {
          domId: 'player' //DOM元素
        }).getAPI() // getAPI后可全局调用__g
      }

      /**
       * 自适应分辨率
       * @returns {Promise<void>}
       */
      const onResize = async () => {
        let playerView = document.getElementById('player')
        playerView.style.height = window.innerHeight + 'px'
        playerView.style.width = window.innerWidth + 'px'
      }

      // 页面加载调用
      window.addEventListener('load', OnLoad, true)

      // 页面变换调用
      window.addEventListener('resize', onResize, true)

      //页面离开事件(离开时清除此页面的操作及重置场景)
      window.onunload = function (e) {
        __g.tools.stopViewDomeAnalysis()
        // 重置场景
        __g.reset()
      }
    </script>
  </body>
</html>
